Skip to content

Refactor PowerMockWhiteboxToJavaReflection into per-API recipes#1021

Merged
MBoegers merged 4 commits into
mainfrom
MBoegers/powermock-whitebox-reflection-split
Jun 17, 2026
Merged

Refactor PowerMockWhiteboxToJavaReflection into per-API recipes#1021
MBoegers merged 4 commits into
mainfrom
MBoegers/powermock-whitebox-reflection-split

Conversation

@MBoegers

@MBoegers MBoegers commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

What

Refactors the released PowerMockWhiteboxToJavaReflection into three focused per-API recipes, aggregated under the existing recipe name as a declarative composite:

  • PowerMockWhiteboxSetInternalStateToJavaReflection
  • PowerMockWhiteboxGetInternalStateToJavaReflection
  • PowerMockWhiteboxInvokeMethodToJavaReflection

The released org.openrewrite.java.testing.mockito.PowerMockWhiteboxToJavaReflection name is preserved as a YAML composite in powermockito.yml, so by-name usage and the ReplacePowerMockito reference are unaffected. Behavior is unchanged — the same three APIs (setInternalState, getInternalState, invokeMethod) migrate exactly as before.

PowerMockWhiteboxSetInternalStateToJavaReflection also handles the 4-arg Whitebox.setInternalState(target, field, value, Class) where-overload, using the supplied Class argument as the getDeclaredField receiver to correctly resolve fields declared on a superclass.

How

  • Shared statement-rewriting machinery lives in a package-private WhiteboxToReflectionVisitor, configured per recipe (constructor-provided MethodMatcher + target java.lang.reflect import).
  • Each recipe gates its visitor with UsesMethod on its own matcher.
  • WhiteboxToReflectionVisitor provides both fieldLookupPrefix (3-arg path: target.getClass().getDeclaredField) and fieldLookupPrefixWhere (4-arg path: whereClass.getDeclaredField) helpers.

Tests

  • Each recipe has its own test class with a @DocumentExample; the existing test cases are preserved verbatim, split by API.
  • PowerMockWhiteboxToJavaReflectionTest is retained as the aggregate test (via recipeFromResources) plus a mixed-API test exercising the composite.
  • setInternalStateWithWhereClass and setInternalStateWithWhereClassVariable tests cover the 4-arg overload in both the per-recipe and aggregate test classes.
  • recipeCsvValidate passes.

Notes

Incorporates the 4-arg setInternalState support from main alongside the per-API refactor. Broader Whitebox coverage (static invokeMethod, getField / getMethod / invokeConstructor) will follow in a separate PR.

Restructure the monolithic `PowerMockWhiteboxToJavaReflection` recipe into six
focused, independently-usable per-API recipes that share a configurable,
package-private `WhiteboxToReflectionVisitor`. The released
`org.openrewrite.java.testing.mockito.PowerMockWhiteboxToJavaReflection` name is
preserved as a declarative YAML composite listing the six, so by-name usage and
the `ReplacePowerMockito` reference are unaffected.

This also expands Whitebox coverage: the `Class where` overloads of
`set`/`getInternalState`, static `invokeMethod`, and `getField`/`getMethod`/
`invokeConstructor`.

Each recipe gates its visitor with `UsesMethod` on its own matchers, and the
shared visitor is configured per recipe (matchers + target reflect import)
rather than via inheritance hooks.
Drop the getField, getMethod and invokeConstructor recipes and the
where-overload / static-invokeMethod handling, so this PR is a pure refactor of
the released PowerMockWhiteboxToJavaReflection recipe (setInternalState,
getInternalState, invokeMethod) into three per-API recipes plus the shared
WhiteboxToReflectionVisitor, with no behavior change versus main. The broader
API coverage will follow in a separate PR.
@MBoegers MBoegers changed the title Split PowerMockWhiteboxToJavaReflection into per-API recipes Refactor PowerMockWhiteboxToJavaReflection into per-API recipes Jun 16, 2026
Comment on lines +70 to +74
return prefix + sink.castType + " " + sink.varName + " = (" + sink.castType + ") " + varName + ".get(#{any(java.lang.Object)});";
}
return prefix + "Object " + sink.varName + " = " + varName + ".get(#{any(java.lang.Object)});";
}
return prefix + varName + ".get(#{any(java.lang.Object)});";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this one use concatenation but the next file use StringBuilder?

@timtebeek

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in the latest two commits. The merge brings in the 4-arg Whitebox.setInternalState(target, field, value, Class) support from main (#1023):

  • The monolithic PowerMockWhiteboxToJavaReflection.java remains deleted (YAML composite in powermockito.yml is unchanged).
  • PowerMockWhiteboxSetInternalStateToJavaReflection now handles both the 3-arg and 4-arg where-overload via a second MethodMatcher; the 4-arg path uses the new fieldLookupPrefixWhere helper in WhiteboxToReflectionVisitor.
  • The setInternalStateWithWhereClass and setInternalStateWithWhereClassVariable tests from main are included in both the per-recipe test class and the aggregate PowerMockWhiteboxToJavaReflectionTest.

Copilot AI requested a review from timtebeek June 16, 2026 18:32
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jun 16, 2026
@MBoegers MBoegers merged commit 9e86af9 into main Jun 17, 2026
1 check passed
@MBoegers MBoegers deleted the MBoegers/powermock-whitebox-reflection-split branch June 17, 2026 05:54
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 17, 2026
mergify Bot added a commit to robfrank/linklift that referenced this pull request Jun 18, 2026
…rom 3.37.0 to 3.38.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-testing-frameworks](https://github.com/openrewrite/rewrite-testing-frameworks) from 3.37.0 to 3.38.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-testing-frameworks's releases](https://github.com/openrewrite/rewrite-testing-frameworks/releases).*

> 3.38.0
> ------
>
> What's Changed
> --------------
>
> * Don't add MockitoExtension during migration when unsafe ([#875](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/875)) by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#1017](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1017)
> * Add junit-platform-launcher testRuntimeOnly dependency for Gradle during JUnit 6 migration by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#1018](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1018)
> * Migrate removed LocalStack Service enum and getEndpointOverride in Testcontainers 2.x migration by [`@​MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-testing-frameworks#1014](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1014)
> * Re-include CloseUnclosedStaticMocks in Mockito1to4Migration by [`@​steve-aom-elliott`](https://github.com/steve-aom-elliott) in [openrewrite/rewrite-testing-frameworks#1019](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1019)
> * Update MockWebServer MockResponse test for nested-type render change by [`@​steve-aom-elliott`](https://github.com/steve-aom-elliott) in [openrewrite/rewrite-testing-frameworks#1020](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1020)
> * Fix UsesType precondition pattern in KotlinTestMethodsShouldReturnUnit by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#1022](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1022)
> * Support 4-arg Whitebox.setInternalState(target, field, value, Class) by [`@​MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-testing-frameworks#1023](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1023)
> * Refactor `PowerMockWhiteboxToJavaReflection` into per-API recipes by [`@​MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-testing-frameworks#1021](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1021)
> * Box primitive casts in `Whitebox.getInternalState`/`invokeMethod` reflection by [`@​MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-testing-frameworks#1024](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/1024)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.37.0...v3.38.0>


Commits

* [`862bd49`](openrewrite/rewrite-testing-frameworks@862bd49) Box primitive casts in Whitebox getInternalState/invokeMethod reflection ([#1024](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/1024))
* [`9e86af9`](openrewrite/rewrite-testing-frameworks@9e86af9) Refactor `PowerMockWhiteboxToJavaReflection` into per-API recipes ([#1021](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/1021))
* [`30c392d`](openrewrite/rewrite-testing-frameworks@30c392d) Support 4-arg Whitebox.setInternalState(target, field, value, Class) ([#1023](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/1023))
* [`c9cd3bd`](openrewrite/rewrite-testing-frameworks@c9cd3bd) Fix UsesType precondition pattern in KotlinTestMethodsShouldReturnUnit ([#1022](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/1022))
* [`a602b44`](openrewrite/rewrite-testing-frameworks@a602b44) OpenRewrite recipe best practices
* [`ff368ff`](openrewrite/rewrite-testing-frameworks@ff368ff) Update UpdateMockWebServerMockResponse test for nested-type render change ([#1](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/1)...
* [`45342dc`](openrewrite/rewrite-testing-frameworks@45342dc) Re-include CloseUnclosedStaticMocks in Mockito1to4Migration ([#1019](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/1019))
* [`e1cb892`](openrewrite/rewrite-testing-frameworks@e1cb892) Migrate removed LocalStack `Service` enum and `getEndpointOverride` in Testco...
* [`3934d1e`](openrewrite/rewrite-testing-frameworks@3934d1e) Add junit-platform-launcher testRuntimeOnly dependency for Gradle during JUni...
* [`d5554a9`](openrewrite/rewrite-testing-frameworks@d5554a9) Don't add MockitoExtension during migration when unsafe ([#875](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/875)) ([#1017](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/1017))
* Additional commits viewable in [compare view](openrewrite/rewrite-testing-frameworks@v3.37.0...v3.38.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants